﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Common;
using System.Linq;
using System.Text;
using Dataglot;

namespace IrishSettings.Commands
{
    public class SelectSchemaByApplicationNameAndVersion:GenericCommand
    {


        public SelectSchemaByApplicationNameAndVersion(DataFactory factory)
            : base(factory)
        {
            Construct();
        }

        private void Construct()
        {
            const string sql = @"
SELECT     {$ns}_settings_schema.* 
FROM         {$ns}_settings_schema inner join {$ns}_settings_applications on 
    {$ns}_settings_schema.application_id= {$ns}_settings_applications.id
WHERE 
    application_id = @applicationId
and
    version= @version
";
            ComposeSql(sql);
            AddParameter("@applicationId", DbType.Int64);
            AddParameter("@version", DbType.String);
        }

        public SelectSchemaByApplicationNameAndVersion(DataFactory factory, DbConnection shared, DbTransaction transaction)
            : base(factory, shared, transaction)
        {
            Construct();
        }

        public DbDataReader SelectRows(long applicationId, Version version)
        {
            if (applicationId == 0)
                throw new ArgumentException("Missing app Id", "applicationId");

            if (version==null)
                throw new ArgumentException("Missing version", "version");

            command.Parameters["@applicationId"].Value = applicationId;
            command.Parameters["@version"].Value = version.ToString();
            return ExecuteReader(CommandBehavior.SingleResult);
        }
    }
}
